package com.blb.shiro3_1;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

public class App {

	public static void main(String[] args) {
		
		
		DefaultSecurityManager securityManager = new DefaultSecurityManager();
		//设置自定义realm
		securityManager.setRealm(new MyRealm());
		//3、把SecurityManager放入SecurityUtils
		SecurityUtils.setSecurityManager(securityManager);
		
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken("root","root");
				
		try{
			subject.login(token);
		}catch (UnknownAccountException uae) {
			System.err.println("用户名不存在");
		} catch (IncorrectCredentialsException ice) {
			System.err.println("密码错误");
		} catch (LockedAccountException lae) {
			System.err.println("账号被冻结");
		} catch (AuthenticationException ae) {
			ae.printStackTrace();
			System.err.println("其它错误");
		}
		
		if(subject.isAuthenticated()){
			System.out.println("登录成功");
			if(subject.hasRole("管理员")){
				System.out.println("有管理员角色");
			}
			if(subject.isPermitted("role:list")){
				System.out.println("有role权限");
			}
			
		}
	}
}
